package com.demo.sso.controller;

import com.demo.sso.common.config.Audience;
import com.demo.sso.common.constant.Const;
import com.demo.sso.enity.LoginUser;
import com.demo.sso.reids.RedisManager;
import com.demo.sso.util.JwtHelper;
import io.jsonwebtoken.Claims;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 请添加模块注释
 *
 * @author fengjy
 * @date 2018-10-28 21:03
 */

@RestController
public class HelloController {

    @Autowired
    private RedisManager redisManager;

    @Autowired
    private Audience audience;

    @GetMapping("/hello")
    public String hello(@RequestParam String token) {

        if (null == token) {
            return "请登录！";
        }

        Claims claims = JwtHelper.parseJWT(token, audience.getBase64Secret());

        Subject subject = SecurityUtils.getSubject();
        org.apache.shiro.session.Session session = subject.getSession();

//        Serializable sessionId = subject.getSession().getId();
//        org.apache.shiro.session.Session session = ShiroSessionRedisUtil.getSession(sessionId);
        LoginUser user = (LoginUser)session.getAttribute(Const.SESSION_USER + claims.get("userid"));
        if (null == user) {
            return "请登录！";
        }

        return token;
    }
}
